import * as dto from './DTO';
import {request, handleFetchResultNotification, REQUEST_TYPE} from '../utils';
import stringify from 'Shared/utils/stringify';
import {formatMessage} from './intl';

const baseUrl = '/parts/api/v1';

const getListDesc = formatMessage({
    id: 'api.message.getListDesc',
    defaultMessage: '查询备件箱单'
});
const getDetailDesc = formatMessage({
    id: 'api.message.getDetailDesc',
    defaultMessage: '查询SAP销售订单详情'
});
const orderDesc = formatMessage({
    id: 'api.message.orderDesc',
    defaultMessage: 'SAP销售订单要单'
});
const getDealersDesc = formatMessage({
    id: 'api.message.getDealersDesc',
    defaultMessage: '查询总代信息'
});
//初始化
export const getInitData = () => request(`${baseUrl}/ui/partInvoice/init`, null, dto.InitDataStruct)
    .then(handleFetchResultNotification(REQUEST_TYPE.initQuery, getListDesc));
// 查询订单列表
export const getList = pageOption => {
    const queryString = stringify(pageOption);
    return request(`${baseUrl}/partInvoices?${queryString}`, null, dto.List)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, getListDesc));
};
// 查询详情
export const getDetail = id =>
    request(`${baseUrl}/partInvoices/${id}`, null, dto.Detail)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, getDetailDesc));
//要单
export const getOrder = data => request(`${baseUrl}/partInvoices`, {
    method: 'POST',
    headers: {'Content-Type': 'application/json'},
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, orderDesc));
// 快捷查询总代
export const getDealersQuickly = pageOption => {
    const queryString = stringify(pageOption);
    return request(`${baseUrl}/dealers/forSubDealer/simple?${queryString}`, null, dto.DealerSimpleList)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, getDealersDesc));
};
// 查询总代
export const getDealers = pageOption => {
    const queryString = stringify(pageOption);
    return request(`${baseUrl}/dealers/forSubDealer?${queryString}`, null, dto.DealerList)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, getDealersDesc));
};
